//删除的时候，要优先删除3k个字符相连的，然后再删除3k+1个字符相连的，然后再删除3k+2个字符相连的
public class Solution420 {
    public int strongPasswordChecker(String password) {
        boolean flagA=false,flagB=false,flagC=false;
        int delete=0,insert=0,change=0;
        StringBuilder stringBuilder=new StringBuilder(password);
        for (int i=0;i<stringBuilder.length();i++){
            if (stringBuilder.charAt(i)>='0' && stringBuilder.charAt(i)<='9'){
                flagC=true;
            }else if (stringBuilder.charAt(i)>='a' && stringBuilder.charAt(i)<='z'){
                flagB=true;
            }else if (stringBuilder.charAt(i)>='A' && stringBuilder.charAt(i)<='Z'){
                flagA=true;
            }
        }
        int req=3;
        if (flagA){
            req--;
        }
        if (flagB){
            req--;
        }
        if (flagC){
            req--;
        }
        if (password.length()>20){
            while (stringBuilder.length()>20){
                int cur=1;
                int max=-1;
                int record=-1;
                int maxx=-1;
                int recordd=-1;
                boolean f=false;
                boolean d=false;
                for (int i=1;i<stringBuilder.length();i++){
                    if (stringBuilder.charAt(i)==stringBuilder.charAt(i-1)){
                        cur++;
                    }
                    if (stringBuilder.charAt(i)!=stringBuilder.charAt(i-1) || i==stringBuilder.length()-1){
                        if (cur>=3 && cur%3==0){
                            delete++;
                            stringBuilder.deleteCharAt(i-1);
                            d=true;
                            f=true;
                            break;
                        }
                        if (cur>=3){
                            f=true;
                            if (cur%3==1) {
                                if (cur>max) {
                                    max=cur;
                                    record=i-1;
                                }
                            }else if (cur%3==2){
                                if (cur>maxx) {
                                    maxx=cur;
                                    recordd=i-1;
                                }
                            }
                        }
                        cur=1;
                    }
                }
                if (!f){
                    break;
                }
                if (!d){
                    delete++;
                    if (record!=-1){
                        stringBuilder.deleteCharAt(record);
                    }else{
                        stringBuilder.deleteCharAt(recordd);
                    }
                }
            }
            if (stringBuilder.length()>20){
                return delete+stringBuilder.length()-20+req;
            }
            for (int i=2;i<stringBuilder.length();i++){
                if (stringBuilder.charAt(i)==stringBuilder.charAt(i-1)&&stringBuilder.charAt(i-1)==stringBuilder.charAt(i-2)){
                    stringBuilder.setCharAt(i,'#');
                    change++;
                }
            }
            return delete+Math.max(change,req);
        }else if (password.length()<6){
            for (int i=2;i<stringBuilder.length();i++){
                if (stringBuilder.charAt(i)==stringBuilder.charAt(i-1)&&stringBuilder.charAt(i-1)==stringBuilder.charAt(i-2)){
                    stringBuilder.insert(i,'#');
                    insert++;
                    if (stringBuilder.length()==6){
                        break;
                    }
                }
            }
            if (stringBuilder.length()<6){
                return Math.max(insert+6-stringBuilder.length(),req);
            }
            for (int i=2;i<stringBuilder.length();i++){
                if (stringBuilder.charAt(i)==stringBuilder.charAt(i-1)&&stringBuilder.charAt(i-1)==stringBuilder.charAt(i-2)){
                    stringBuilder.setCharAt(i,'#');
                    change++;
                }
            }
            return Math.max(insert+change,req);
        }else{
            for (int i=2;i<stringBuilder.length();i++){
                if (stringBuilder.charAt(i)==stringBuilder.charAt(i-1)&&stringBuilder.charAt(i-1)==stringBuilder.charAt(i-2)){
                    stringBuilder.setCharAt(i,'#');
                    change++;
                }
            }
            return Math.max(change,req);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution420().strongPasswordChecker("A1234567890aaabbbbccccc"));
    }
}
